package fhfl.mobapp.belastungstest.mobapp_communicate;

import java.nio.ByteBuffer;

/**
 * Created by Iwo on 05.06.2014.
 */
public class ContentFactory
{
    /**
     * Größe der Datenmenge in Byte
     */
    private int packetSize;
    private int packetNumber = 0;
    private byte[] data;

    /**
     * Constructor which set the packetSize
     * @param packetSize Die Größe des Packets in Byte
     */
    public ContentFactory(int packetSize)
    {
        this.packetSize = packetSize;
        data = new byte[this.packetSize];
        createData();
    }

    public void createData()
    {
        for (int i = MagicNumbers.DATE_LENGTH + MagicNumbers.PACKET_NUMBER_LENGTH; i < data.length ; i++)
            this.data[i] = MagicNumbers.CONTENT;
    }

    public byte[] setPackage()
    {
        byte[] time = ByteBuffer.allocate(MagicNumbers.DATE_LENGTH).putLong( new Long(System.currentTimeMillis())).array();

        for (int i = 0; i < MagicNumbers.DATE_LENGTH; i++)
            this.data[i] = time[i];

        if (packetNumber < Integer.MAX_VALUE)
            packetNumber++;
        else
            packetNumber = 0;

        byte[] number = ByteBuffer.allocate(MagicNumbers.PACKET_NUMBER_LENGTH).putInt(packetNumber).array();
        for (int i = 0; i < MagicNumbers.PACKET_NUMBER_LENGTH; i++)
            this.data[i + MagicNumbers.DATE_LENGTH] = number[i];
        
        return data;
    }

    /**
     * Gibt die Packetgröße in Byte zurück.
     * @return Die Packetgröße in Byte
     */
    public int getPacketSize()
    {
        return packetSize;
    }
}
